package com.mebs.edi.parser.utils;

import java.io.File;

/**
 * 
 * Utility functions for working with Files
 * 
 * @author nit
 * @version 1.0
 *
 */
public class FileUtils {
	
	private static final String EXTENSION_SEPARATOR = ".";
	
	/**
	 * 
	 * Returns file extension or empty string if file has no extension
	 * 
	 * @param file
	 * @return extension String 
	 * @throws IllegalArgumentException if passed argument is not a valid file
	 *  
	 */
	public static String getExtension(File file) {
		if (file == null || !file.isFile()) {
			throw new IllegalArgumentException("Invalid file");
		}
		String fullPath = file.getAbsolutePath();
		int dot = fullPath.lastIndexOf(EXTENSION_SEPARATOR);
	    return (dot != -1) ? fullPath.substring(dot + 1) : "";
	}
	
	/**
	 * 
	 * Checks that file is not null, exists, is not a directory, and has the right extension 
	 * 
	 * @param file
	 * @return true if the file is valid, false otherwise
	 *  
	 */
	public static boolean isValidFile(File file, String extension) {
		if (file == null || !file.isFile()) {
			return false;
		} else if (!extension.equalsIgnoreCase(getExtension(file)) ) {
			return false;
		}
		return true;
	}
	
}
